package com.cmcc.passenger.ui.order.viewmodel

import androidx.databinding.ObservableField
import androidx.lifecycle.*
import com.cmcc.passenger.data.AppRequest
import com.cmcc.passenger.data.Network
import com.cmcc.passenger.data.entity.OrderDetail
import com.cmcc.passenger.data.entity.request.EvaluateRequest
import com.cmcc.shared.model.AppResponse
import com.cmcc.shared.model.DriverCache
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class OrderDetailViewModel : ViewModel() {
    val orderDetail = MutableLiveData<OrderDetail>()

    val avatarUrl = ObservableField<String>()

    val directionCode = MutableLiveData<Int>()
    val direction = MutableLiveData<String>()
    val distance = MutableLiveData<String>()
    val duration = MutableLiveData<String>()

    val geoLocation = MutableLiveData<String>()

    val driverCache = MutableLiveData<DriverCache>()

    val evaluateData = MutableLiveData<AppResponse<String>>()

    val finishEven : LiveData<Boolean> = evaluateData.switchMap {
        liveData {
            delay(1500)
            emit(it != null && it.code == 200)
        }
    }
    /**
     * 获取订单详情
     */
    fun getOrderDetail(id: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getOrderDetail(id)
                if (response.isSuccessful) {
                    val data = response.body()
                    orderDetail.value = data?.data
//                    L.d("data:"+data?.data)
//                    avatarUrl.set(data?.data?.driver?.headPortraitAddress?:"")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    fun geocode(lat: Double, lng: Double) {
        viewModelScope.launch(Dispatchers.Main) {
            val postRequest = Network.getInstance().googleApiService.geocode("$lat,$lng")
            try {
                val response = postRequest.await()
                if (response.isSuccessful) {
                    val posts = response.body()
                    if (!posts?.results.isNullOrEmpty()) {
                        L.d(posts.toString())
                        geoLocation.value =
                            posts?.results?.get(0)?.address_components?.get(0)?.long_name
                    }
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    fun getDirection(origin: String, destination: String) {
        viewModelScope.launch(Dispatchers.Main) {
            val postRequest =
                Network.getInstance().googleApiService.getDirection(origin, destination)
            try {
                val response = postRequest.await()
                if (response.isSuccessful) {
                    val posts = response.body()
                    if (!posts?.routes.isNullOrEmpty()) {
                        distance.value = posts?.routes?.get(0)?.legs?.get(0)?.distance?.text
                        duration.value = posts?.routes?.get(0)?.legs?.get(0)?.duration?.text
                        direction.value = posts?.routes?.get(0)?.overview_polyline?.points
                    }else{
                        directionCode.value = 1
                    }
                } else {
                    L.d(response.errorBody().toString())
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }


    fun getDriverCache(driverId: String) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = AppRequest.getInstance().getDriverCache(driverId)
                if (response.isSuccessful) {
                    val data = response.body()
                    driverCache.value = data?.data

                    L.d("----: $driverCache")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }
    /**
     * 评价接口
     */
    fun evaluate(request: EvaluateRequest) :MutableLiveData<AppResponse<String>>{
        viewModelScope.launch(Dispatchers.Main) {

            val response = AppRequest.getInstance().evaluate(request)

            if (response.isSuccessful) {
                evaluateData.value = response.body()
            }


        }
        return evaluateData
    }

}